home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-07-15 | 5.7 KB | 217 lines | [TEXT/MPS ] |
- // UMouseTrackBehavior.cp
- // Copyright © 1992 by Apple Computer, Inc. All rights reserved.
- // Kent Sandvik DTS
- // This file contains the basic TMouseTrackBehavior member functions
- // Version Info (latest first):
- //
- // <1> khs 1.0 First final version
- // <2> khs 1.0.1 Fixed a memory leak in TMapApplication::GetSleepValue()
-
-
- #ifndef __MOUSETRACKBEHAVIOR__
- #include "UMouseTrackBehavior.h"
- #endif
-
-
- // Initialize needed parts for the MouseTrackBehavior modules
- #pragma segment AInit
- pascal void InitMouseTrackBehavior()
- {
- if (gDeadStripSuppression)
- {
- macroDontDeadStrip(TMouseTrackBehavior);
- macroDontDeadStrip(TTrackWindow);
- }
-
- RegisterStdType("TTrackWindow", 'ttrk');
- }
-
-
- // Close the remove the single swallow application when closing the floating window
- #pragma segment AClose
- pascal void TTrackWindow::Close()
- {
- gMouseTrackWindow = NULL; // signal that it's OK to open one again
-
- gApplication->RemoveBehavior(fBehavior); // Get rid of the TSwallowBehavior in gApplication...
-
- inherited::Close();
- }
-
-
- // Get the pointer to the single swallow behavior which we need when removing it
- #pragma segment ARes
- pascal void TTrackWindow::GetOriginatorBehavior(TSwallowBehavior* behavior)
- {
- fBehavior = behavior;
- }
-
-
- // Create behavior and define default values
- #pragma segment AInit
- pascal void TMouseTrackBehavior::IMouseTrackBehavior(ResNumber menuID)
- {
- this->IBehavior(kMouseTrackBehavior);
- this->SetIdleFreq(0); // call as often as possible
- this->SetEnabled(TRUE); // enable behavior
- fMenuID = menuID;
- }
-
-
- // Initialized any static fields in the behavior
- #pragma segment AInit
- pascal void TMouseTrackBehavior::Initialize()
- {
- inherited::Initialize();
- fMenuID = 0;
- }
-
-
- // Setup needed menus dynamically, i.e. mouse tracking menu if floating window not present
- #pragma segment ARes
- pascal void TMouseTrackBehavior::DoSetupMenus()
- {
- inherited::DoSetupMenus();
- Enable(cOpenTracker, gMouseTrackWindow == NULL);
- }
-
-
- // Do menu commands associated with the mousetrack behavior, in this case only one
- // menu, open floating window and install the event swallowing behavior
- #pragma segment ASelCommand
- pascal void TMouseTrackBehavior::DoMenuCommand(CommandNumber theNum)
- {
- if (theNum == cOpenTracker && gMouseTrackWindow == NULL)
- {
- this->CreateWindoid(); // create windoid on the flight
- this->CreateSwallowBehavior(); // create and install event behavior
- gMouseTrackWindow->Open(); // open windoid on screen
- Enable(cOpenTracker, FALSE); // disable the menu entry
- }
- else
- inherited::DoMenuCommand(theNum);
- }
-
-
- // Create windoid, only one which is used by all open documents
- #pragma segment AInit
- pascal void TMouseTrackBehavior::CreateWindoid()
- {
- FailInfo fi;
- TTrackWindow * aWindow = NULL;
-
- if (fi.Try())
- {
- // Create Windoid
- aWindow = (TTrackWindow *)gViewServer->NewTemplateWindow(kFloatingWindow, NULL);
- FailNIL(aWindow);
- gMouseTrackWindow = aWindow; // register the windoid globally
-
- fi.Success();
- }
- else
- fi.ReSignal();
- }
-
-
- // Create swallow behavior which will eat all events, also install it to the main
- // gApplication framework and let the floating window let know about this behavior
- #pragma segment AInit
- pascal void TMouseTrackBehavior::CreateSwallowBehavior()
- {
- // Install swallow behavior which takes *every* event!
- TSwallowBehavior * aSwallowBehavior = new TSwallowBehavior;
- aSwallowBehavior->ISwallowBehavior();
- gApplication->AddBehavior(aSwallowBehavior);
- gMouseTrackWindow->GetOriginatorBehavior(aSwallowBehavior);// windoid needs to know the current behavior
- }
-
-
- // Initialize the swallow behavior, it should find out the resources to the floating
- // window (fields) which it needs when it updates them
- #pragma segment AInit
- pascal void TSwallowBehavior::ISwallowBehavior()
- {
- this->IBehavior(kNoIdentifier);
- this->SetIdleFreq(0); // call as often as possible
-
- fTrackWindow = gMouseTrackWindow; // get the global/only Windoid address
-
- // Get Resource pointers
- fMouseStatus = (TStaticText *)fTrackWindow->FindSubView('mous');
- fVertical = (TNumberText *)fTrackWindow->FindSubView('numv');
- fHorizontal = (TNumberText *)fTrackWindow->FindSubView('numh');
- }
-
-
- // Get access to all events, and trigger actions based on key events, in our
- // case mouseUp and mouseDowns
- #pragma segment ARes
- pascal Boolean TSwallowBehavior::DoToolboxEvent(TToolboxEvent* event)
- {
- WindowPtr aWMgrWindow;
-
- short whereMouseDown = FindWindow(event->fEventRecord.where, aWMgrWindow);
- TWindow * aWindow = gApplication->WMgrToWindow(aWMgrWindow);
-
- if (event && (event->fIdentifier == mouseDown))
- fEventType = mouseDown;
-
- else if (event && (event->fIdentifier == mouseUp))
- fEventType = mouseUp;
-
- else if (event && (event->fIdentifier == activateEvt))
- fEventType = mouseUp;
-
- if (aWindow)
- {
- VPoint theMouse(event->fEventRecord.where);
- aWindow->SuperToLocal(theMouse);
- fMouseVPoint = theMouse;
- }
-
- VRect dummy;
- if (gMouseTrackWindow != NULL)
- this->Draw(dummy); // only draw if windoid exists
-
- return inherited::DoToolboxEvent(event);
- }
-
-
- // Draw contents inside floating window
- #pragma segment ASelCommand
- pascal void TSwallowBehavior::Draw(const VRect& area)
- {
- if (gMouseTrackWindow != NULL)
- {
- // Draw Mouse status
- if (fEventType == mouseDown)
- {
- fMouseStatus->SetText("MouseDown", kRedraw);
- fTrackWindow->Update();
- }
- else if (fEventType == mouseUp)
- {
- fMouseStatus->SetText("MouseUp", kRedraw);
- fTrackWindow->Update();
- }
-
- // Draw coordinates
- // Avoid de-referencing problems by using stack based variables for NumToString
- CStr255 tempH, tempV;
- VPoint tempVP;
-
- tempVP = fMouseVPoint;
-
- NumToString(tempVP.h, tempH);
- NumToString(tempVP.v, tempV);
- fHorizontal->SetText(tempH, kRedraw);
- fVertical->SetText(tempV, kRedraw);
- fTrackWindow->Update();
- }
-
- inherited::Draw(area);
- }
-
-
-